package com.neu.question.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neu.question.domain.EndUserInfo;
import com.neu.question.domain.TenantsPropertyInfo;
import com.neu.question.domain.UserInfo;
import com.neu.question.form.TenantsForm;
import com.neu.question.service.TenantsPropertyInfoService;
import com.neu.question.mapper.TenantsPropertyInfoMapper;
import java.util.List;
import java.util.Map;

import com.neu.question.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author DELL
 * @description 针对表【tenants_property_info】的数据库操作Service实现
 * @createDate 2022-10-31 19:09:10
 */
@Service
public class TenantsPropertyInfoServiceImpl extends ServiceImpl<TenantsPropertyInfoMapper, TenantsPropertyInfo>
  implements TenantsPropertyInfoService {

  @Autowired
  private UserInfoService userInfoService;

  @Autowired
  private TenantsPropertyInfoService tenantsPropertyInfoService;



  @Override
  public List<Map<String, Object>> selectAllTenantsPropertyInfo() {
    return baseMapper.selectAllTenantsPropertyInfo();
  }

  @Override
  public Map<String, Object> selectTenantsPropertyInfoByUserId(String id) {
    return baseMapper.selectTenantsPropertyInfoByUserId(id);
  }

  @Override
  public int addTenantsPropertyInfo(Map<String, Object> map) {
    int k=baseMapper.addTenantsPropertyInfo(map);
    return 1;
  }

  @Override
  public int update(TenantsForm tenantsForm) {
    QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
    //被修改的用户
    String oldName = tenantsForm.getOldUsername();
    String oldPhone = tenantsForm.getOldPhoneNum();
    queryWrapper.eq("username", tenantsForm.getOldUsername());
    //被修改的用户
    UserInfo one1 = userInfoService.getOne(queryWrapper);
    //判断用户名
    if (!oldName.equals(tenantsForm.getUsername())) {
      queryWrapper.clear();
      queryWrapper.eq("username", tenantsForm.getUsername());
      //如果有重名
      if (userInfoService.getOne(queryWrapper) != null) {
        //重名
        return 3;
      }
      one1.setUsername(tenantsForm.getUsername());
    }
    if (!oldPhone.equals(tenantsForm.getPhoneNum())) {
      queryWrapper.clear();
      queryWrapper.eq("phone_num", tenantsForm.getPhoneNum());
      if (userInfoService.getOne(queryWrapper) != null) {
        //重复电话号
        return 4;
      }
      one1.setPhoneNum(tenantsForm.getPhoneNum());
    }
    one1.setPassword(tenantsForm.getPassword());
    if (! userInfoService.saveOrUpdate(one1)) {
      //user存储失败
      return 5;
    }
    //成功
    return 1;
  }
}




